package com.trafficsys.modules.police.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.trafficsys.modules.entity.*;
import com.trafficsys.modules.police.pojo.DriverAndIdCard;
import com.trafficsys.modules.police.pojo.PoliceAndIdCard;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

@Mapper
@Repository
public interface DriverAndIdCardDao extends BaseMapper<Driver> {
    @Select("SELECT i.id,d.card_id,phone,address,birthday,department,i.name,state,validity" +
            " from driver d LEFT JOIN id_card i ON d.card_id=i.card_id")
    DriverAndIdCard selectDriverAndCardId();


    //分页查询
    @Select("select * from driver d LEFT JOIN id_card i ON d.card_id=i.card_id")
    IPage<DriverAndIdCard> selectDriverByCardId(IPage<DriverAndIdCard> page);

    //修改驾驶员
    @Update("update driver set phone=#{phone} where card_id=#{cardId}")
    int updateDriver(DriverAndIdCard driverAndIdCard);

    //修改idCard
    @Update("update id_card set address=#{address},birthday=#{birthday},department=#{department}" +
            ",name=#{name},state=#{state},validity=#{validity} where card_id=#{cardId}")
    int updateIdCard(DriverAndIdCard driverAndIdCard);

    //删除DriverAndIdCard
    @Delete("delete from driver where card_id=#{cardId}")
    int deleteDriver(String cardId);

    //分页查询驾驶员驾驶证
    @Select("select * from  drive_card")
    IPage<DriveCard> getDriveCard(IPage<DriveCard> page);

    //分页查询警员
    @Select("select * from  police p LEFT JOIN id_card i ON p.card_id = i.card_id")
    IPage<PoliceAndIdCard> getPolice(IPage<PoliceAndIdCard> page);

    //修改警员
    @Update("update police set phone=#{phone} where card_id=#{cardId}")
    int updatePolice(PoliceAndIdCard policeAndIdCard);

    //修改idCard
    @Update("update id_card set address=#{address},birthday=#{birthday},department=#{department}" +
            ",name=#{name},state=#{state},validity=#{validity} where card_id=#{cardId}")
    int updatePoliceIdCard(PoliceAndIdCard policeAndIdCard);

    //删除PoliceAndIdCard
    @Delete("delete from police where card_id=#{cardId}")
    int deletePolice(String cardId);

    //添加行驶证
    @Insert("insert into travel_card (vin,brand_model,car_clazz,car_no,engine_no,file_no" +
            ",card_id,invalid_date,state,travel_car_id,user_nature) " +
            "values(#{vin},#{brandModel},#{carClazz},#{carNo},#{engineNo},#{fileNo}," +
            "#{cardId},#{invalidDate},#{state},#{travelCarId},#{userNature})")
    int insertTravelCard(TravelCard travelCard);

    //修改行驶证
    @Update("update travel_card set vin=#{vin},brand_model=#{brandModel},car_clazz=#{carClazz}," +
            "engine_no=#{engineNo},file_no=#{fileNo},card_id=#{cardId}," +
            "invalid_date=#{invalidDate},state=#{state},travel_car_id=#{travelCarId}," +
            "user_nature=#{userNature} where car_no=#{carNo}")
    int updateTravelCard(TravelCard travelCard);

    //删除行驶证
    @Delete("delete from travel_card where car_no=#{carNo}")
    int deleteTravelCard(String carNo);

    //添加驾驶证
    @Insert("insert into drive_card (clazz,create_date,department,drive_card_id,card_id" +
            ",nationality,score,state,valid_period) " +
            "values(#{clazz},#{createDate},#{department},#{driveCardId},#{cardId}," +
            "#{nationality},#{score},#{state},#{validPeriod})")
    int insertDriverCard(DriveCard driveCard);

    //修改驾驶证
    @Update("update drive_card set clazz=#{clazz},create_date=#{createDate}," +
            "department=#{department},drive_card_id=#{driveCardId}," +
            "nationality=#{nationality},score=#{score},state=#{state}," +
            "valid_period=#{validPeriod} where card_id=#{cardId}")
    int updateDriverCard(DriveCard driveCard);

    //删除驾驶证
    @Delete("delete from drive_card where card_id=#{cardId}")
    int deleteDriverCard(String cardId);

}
